$(function () {
    let token = sessionStorage.getItem("token");
    let name = sessionStorage.getItem("username");
    $("#name").append(name);
    // 图片列表
    var pic_list = [];
    // 视频列表
    var video_list = [];
    //视频名称
    $('#doc-form-file').on('change', function() {
        var fileNames = '';
        $.each(this.files, function() {
            fileNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#file-list').html(fileNames);
    });



    // 届别
    $.ajax({
        type:"post",
        url:testurl + "due/list",
        headers: { "Authorization": "Bearer " + token},
        contentType:"application/json",
        data: JSON.stringify({
            "pageNum": "1"
        }),
        success:function (data) {
            var options = "";
            var due = data.obj;
            $.each(due,function (index,value) {
                options += '  <option value="'+value.id+'">'+value.name+'</option>';
            });
            $("#doc-select-1").html(options);
            // console.log(data)
        }
    });
    // 会议
    $.ajax({
        type:"post",
        headers: { "Authorization": "Bearer " + token},
        url:testurl + "meeting/list",
        contentType:"application/json",
        data: JSON.stringify({
            "pageNum": "1"
        }),
        success:function (data) {
            var options2 = "";
            var meeting = data.obj;
            $.each(meeting,function (index,value) {
                options2 += '<option value="'+value.id+'">'+value.name+'</option>';
            });
            $("#doc-select-2").html(options2);

        }
    });

    // 提交
    $("#submit").click(function () {




        //表单验证
        var form1 = $('#form1');
        form1.validator({
            submit: function () {
                var formValidity = this.isFormValid();
                if (formValidity){
                    //视频上传
                    var formData2 = new FormData();
                    var pic2 = document.getElementById("doc-form-file").files;

                    for(var n=0;n<pic2.length;n++){
                        formData2.append(""+n+"",pic2[n]);
                    }
                    $.ajax({
                        url : testurl + "duty/record/file",
                        type: 'POST',
                        headers: { "Authorization": "Bearer " + token},
                        data: formData2,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success : function(data) {
                            video_list = data.obj;
                            console.log(data);
                        },
                        error : function(data) {
                            console.log(data);
                        }
                    });


                    // 图片上传
                    var formData = new FormData();
                    var pic = document.getElementById("file").files;

                    for(var j=0;j<pic.length;j++){
                        formData.append(""+j+"",pic[j]);
                    }
                    $.ajax({
                        url : testurl + "duty/record/file",
                        type: 'POST',
                        headers: { "Authorization": "Bearer " + token},
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success : function(data) {
                            pic_list = data.obj;
                            console.log(data);
                        },
                        error : function(data) {
                            console.log(data);
                        }
                    });
                    let title = $("#title").val();
                    var organizer = $("#organizer").val();
                    var duty_date = $("#duty_date").val();
                    var type = $("[name='type']:checked").val();
                    var address = $("#address").val();
                    // 获得select被选中option的value值
                    var value1=$("#doc-select-1 option:selected").val(); //获取选中的项

                    var value2 = $("#doc-select-2 option:selected").val();
                    var content = $("#content").val();

                    var str = {"title":title,"organizer":organizer,"duty_date":duty_date,"type":type,
                        "address":address,"due_id":value1,"meeting_id":value2,"content":content,
                        "pic_list":pic_list,"video_list":video_list};
                    console.log(str);
                    $.ajax({
                        async: false,
                        headers: { "Authorization": "Bearer " + token},
                        type: "post",
                        url: testurl + "duty/record/",
                        contentType: "application/json",
                        data:JSON.stringify(str),
                        success:function (data) {

                            if (data.success === true) {
                                $(".users").html("添加履职");
                                $(".alert").html(data.message);
                                $('#my-alert').modal({target: '#my-alert'});
                                $('#leave').modal('close');
                                $("#return").click(function () {
                                    window.location.href = "lvzhirecord.html";
                                })
                            }else {
                                $(".users").html("添加履职");
                                $(".alert").html(data.message);
                                $('#my-alert').modal({target: '#my-alert'});
                                $('#leave').modal('close');
                                $("#return").click(function () {
                                    window.location.href = "lvzhirecord.html";
                                })
                            }
                        }
                    });
                return false
                } else {
                    return false
                }
            }
        });
    });

});